package com.xdtech.hotel.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xdtech.common.utils.JsonUtil;
import com.xdtech.hotel.service.CardService;
import com.xdtech.hotel.vo.CardItem;
import com.xdtech.hotel.vo.RoomItem;
import com.xdtech.sys.vo.RoleItem;
import com.xdtech.web.model.Pagination;
import com.xdtech.web.model.ResultMessage;

/**
 * 
 * @author max.zheng
 * @create 2015-04-04 11:11:08
 * @since 1.0
 * @see
 */
@Controller
@Scope("prototype")
public class CardController {
	@Autowired
	private CardService cardService;
	@RequestMapping(value="/card.do",params = "card")
	public ModelAndView skipCard() {
		return new ModelAndView("hotel/card/card_ftl");
	}
	
	@RequestMapping(value="/card.do",params="loadList")
	@ResponseBody
	public Map<String, Object> loadList(CardItem item,Pagination pg) {
		Map<String, Object> results =  cardService.loadPageDataByConditions(pg,item,"findCardByCondition");
		return results;
	}
	
	@RequestMapping(value="/card.do",params = "editCard")
	public ModelAndView editCard(HttpServletRequest request,Long cardId) {
		if (cardId!=null) {
			request.setAttribute("cardItem", cardService.loadCardItem(cardId));
		}
		return new ModelAndView("hotel/card/editCard_ftl");
	}
	
	@RequestMapping(value="/card.do",params = "authCard")
	public ModelAndView authCard(HttpServletRequest request,Long cardId) {
		if (cardId!=null) {
			request.setAttribute("cardItem", cardService.loadCardItem(cardId));
			request.setAttribute("cardId", cardId);
		}
		return new ModelAndView("hotel/card/authCard_ftl");
	}
	
	@RequestMapping(value="/card.do",params = "saveCard")
	@ResponseBody
	public ResultMessage saveCard(CardItem item) {
		ResultMessage r = new ResultMessage();
		if (cardService.saveOrUpdateCard(item)) {
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	@RequestMapping(value="/card.do",params = "deleteCardItems")
	@ResponseBody
	public ResultMessage deleteCardItems(String ids) {
		ResultMessage r = new ResultMessage();
		if (StringUtils.isNotEmpty(ids)) {
			String[] tempIds = ids.split(",");
			List<Long> cardIds = new ArrayList<Long>();
			for (String id : tempIds) {
				cardIds.add(Long.valueOf(id));
			}
			cardService.deleteCardInfo(cardIds);
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	@RequestMapping(params = "saveCardToRooms")
	@ResponseBody
	public ResultMessage saveCardToRooms(Long cardId,String data) {
		ResultMessage rm = new ResultMessage(); 
		List<RoomItem> rooms = JsonUtil.getDTOList(data, RoomItem.class);
		cardService.saveCardToRooms(cardId, rooms);
		return rm;
	}

}
